#include "common.h"

/**
 *1.right_num是进入游戏时,伪随机函数给的值
 *2.bigger\smaller\equal是玩家猜测数字和正确数字相比较,分别是更大\更小\相当,分开计数后方便统计猜测次数
 *3.guessnum是玩家猜测的数字
 */
int start(int right_num)
{
	int bigger = 0, smaller = 0, equal = 0;
	int guessnum;

	get_random(&right_num);    

    printf(GREEN"\n请输入1~100之间的数:\n"NONE);

    while(1){
		 while(scanf("%d",&guessnum) != 1 || guessnum < 1 || guessnum > 100){
			   printf(RED"***ERROR, Try again:\n"NONE);
			   while(getchar() != '\n');
		 }
        
		 if(guessnum > right_num) {              
		    printf(RED"Too big, Try again\n"NONE);
            bigger++;  
         }  

		 else if(guessnum < right_num) {              
	             printf(RED"Too small, Try again\n"NONE);  
                 smaller++; 
         } 
      
		 else if (guessnum == right_num){                 
			     equal++;

			     if (bigger+smaller+equal <= 7){    
			         printf(YELLOW);
					 printf("\nCongratulation!\n");
			         printf("没错,答案就是: %d ,猜得挺准啦!", right_num);
				     printf("你猜对答案花费的次数为：%d\n", (bigger+smaller+equal));
    			     printf(NONE);
					 break;
					 
                 }
		         
				 else if(bigger+smaller+equal > 7 && bigger+smaller+equal < 15){                  	     
				         printf(YELLOW);
						 printf("\nI Guess You Can Do It Better!!\n");
				         printf("没错,答案就是: %d ,猜得太慢了吧!", right_num);
			  	         printf("你猜对答案花费的次数为：%d\n", (bigger+smaller+equal));
						 printf(NONE);
    			         break;
						 
				 }    
		         
			     else if(bigger+smaller >= 15){
				         printf(RED"I am sorry, but you are so foolish that I can to continue!\n"NONE);
				         printf(YELLOW"没错,答案就是: %d ,大概你还是要多练习呀!", right_num);
				         printf("你猜对答案花费的次数为：%d\n"NONE,(bigger+smaller+equal));
				         break;
						 	
			     }
			     
			     else {
				      printf(RED"ERROR!\n"NONE);
				      exit(0);
			     }
				 
         } 
		 	
    }


	char YorN ;
	printf(WHITE"\n是否重新开始? y/n :\n"NONE);
    
		setbuf(stdin,NULL);
        scanf("%c",&YorN);
        
		if (YorN == 'y' || YorN == 'Y')
			start(right_num);
        else if (YorN == 'n' || YorN == 'N')
			exit(0);
		else 
			printf(RED"error, input y/n:\n"NONE);
			
	return 0;
}


